<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>后台主页</title>
</head>

<body>
  <h1>首页</h1>
  <p id="ptxt"></p>
  <button id="btn">退出登录</button>
  <script>
    let p=document.getElementById('ptxt')
    fetch('http://localhost:8080/api/getusername').then(res=>res.json()).then(res=>{
      
      if (res.status !== 0) {
          alert('您尚未登录，请登录后再执行此操作！')
          location.href = './login.html'
        } else {
          p.innerHTML='欢迎您：' + res.username
        }
    })

    let btn=document.getElementById('btn')
    btn.addEventListener('click',()=>{
      fetch('http://localhost:8080/api/logout').then(res=>res.json()).then(res=>{
        if (res.status === 0) {
            location.href = './login.html'
          }
      })
    })
  </script>
</body>

</html>